package demo1;


import sun.awt.windows.WPrinterJob;

import java.util.Objects;

class Money implements Cloneable{
    public double money;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Money money1 = (Money) o;
        return Double.compare(money1.money, money) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(money);
    }
}

class Student implements Cloneable{
    int age;
    Money m = new Money();
    public Student(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp =(Student)super.clone();
        tmp.m = (Money)this.m.clone();
        return tmp;
    }

//    @Override
//    public boolean equals(Object obj) {
//        Student student = (Student) obj;
//        return this.age == student.age;
//    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(age, m);
//    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(m, student.m);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, m);
    }
}
public class Test {
    public static void main(String[] args) {
        Student student1 = new Student(19);
        Student student2 = new Student(19);
        System.out.println(student1.hashCode());//hashcode用来确定对象在内存中存储的位置是否相同
        System.out.println(student2.hashCode());
        System.out.println(student1.equals(student2));
    }
    public static void main4(String[] args) {
        Student student1 = new Student(16);
        Student student2 = new Student(16);
        System.out.println(student1==student2);//student1和student2是两个对象，所以==比较的是两个对象的地址
        System.out.println(student1.equals(student2));
    }
    public static void main3(String[] args) {
        Student student1 = new Student(16);
        Class<?> ret = student1.getClass();
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Object obj1 = new Student(19);
        Object obj2 = new Money();//Object引用可以接收所有类的对象
    }
    public static void main1(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student(16);
        student1.m.money =19.9;
        Student student2 = (Student) student1.clone();
        student1.age = 20;
        System.out.println(student1.age);//此时student1的money为20
        System.out.println(student2.age);//student2的money为16

        student2.m.money = 29.9;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);//此时打印的student1和student2的money都为29.9，此时发生了浅拷贝，因为在堆中存放的是m的地址

    }
}
